package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.model.Instance.L1DeInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1ChatParty;
import com.lineage.server.serverpackets.S_Party;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_ChatParty extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_ChatParty.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      String name;
      L1ChatParty chatParty;
      L1PcInstance targetPc;
      L1DeInstance de;
      L1PcInstance[] members;
      int length, i;
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      pc.isGhost();
      if (pc.isTeleport())
        return; 
      int type = readC();
      switch (type) {
        case 0:
          name = readS();
          if (!pc.isInChatParty()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(425));
            return;
          } 
          if (!pc.getChatParty().isLeader(pc)) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(427));
            return;
          } 
          targetPc = World.get().getPlayer(name);
          de = null;
          if (targetPc == null)
            de = C_ChatWhisper.getDe(name); 
          if (targetPc == null && de == null) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(109, name));
            return;
          } 
          if (pc.getId() == targetPc.getId())
            return; 
          length = (members = pc.getChatParty().getMembers()).length;
          i = 0;
          while (i < length) {
            L1PcInstance member = members[i];
            if (member.getName().toLowerCase().equals(name.toLowerCase())) {
              pc.getChatParty().kickMember(member);
              return;
            } 
            i++;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(426, name));
          break;
        case 1:
          if (pc.isInChatParty())
            pc.getChatParty().leaveMember(pc); 
          break;
        case 2:
          chatParty = pc.getChatParty();
          if (pc.isInChatParty()) {
            pc.sendPackets((ServerBasePacket)new S_Party("party", pc.getId(), chatParty.getLeader().getName(), 
                  chatParty.getMembersNameList()));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(425));
          break;
      } 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
